package com.ruoyi.faceplus.face;

import com.arcsoft.face.ActiveFileInfo;
import com.arcsoft.face.EngineConfiguration;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.FunctionConfiguration;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.enums.ErrorInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class FaceEngineHelper {
    
    @Value("${arcpro.appId}")
    private String appId;

    @Value("${arcpro.appKey}")
    private String sdkKey;

    @Value("${arcpro.activeKey}")
    private String activeKey;

    @Value("${arcpro.libPath}")
    private String libPath;

    private boolean mActiveStatus = false;

    // 创建引擎
    public synchronized FaceEngine create() {
        FaceEngine faceEngine = new FaceEngine(libPath);
        // 激活引擎
        active(faceEngine);
        if (!mActiveStatus) return null;
        // 初始化引擎
        int errorCode = faceEngine.init(initConfiguration());
        if (errorCode != ErrorInfo.MOK.getValue()) {
            System.out.println("人脸引擎初始化失败：" + errorCode);
            return null;
        }
        return faceEngine;
    }

    public void active(FaceEngine faceEngine) {
        if (mActiveStatus) return;
        ActiveFileInfo activeFileInfo = new ActiveFileInfo();
        int errorCode = faceEngine.getActiveFileInfo(activeFileInfo);
        if (errorCode != ErrorInfo.MOK.getValue() || errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
            errorCode = faceEngine.activeOnline(appId, sdkKey, activeKey);
            if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
                System.out.println("人脸引擎激活失败：" + errorCode);
                return;
            }
        }
        mActiveStatus = true;
        System.out.println("人脸引擎已激活");
    }

    public EngineConfiguration initConfiguration() {
        // 引擎配置
        EngineConfiguration engineConfiguration = new EngineConfiguration();
        engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
        engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);
        engineConfiguration.setDetectFaceMaxNum(10);
        // 功能配置
        FunctionConfiguration functionConfiguration = new FunctionConfiguration();
        functionConfiguration.setSupportAge(true);
        functionConfiguration.setSupportFaceDetect(true);
        functionConfiguration.setSupportFaceRecognition(true);
        functionConfiguration.setSupportGender(true);
        functionConfiguration.setSupportLiveness(true);
        functionConfiguration.setSupportIRLiveness(true);
        functionConfiguration.setSupportImageQuality(true);
        functionConfiguration.setSupportMaskDetect(true);
        functionConfiguration.setSupportUpdateFaceData(true);
        engineConfiguration.setFunctionConfiguration(functionConfiguration);
        return engineConfiguration;
    }
}
